<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width,user-scalable=yes, minimum-scale=0.4, initial-scale=0.8,target-densitydpi=low-dpi" />

    <link rel="shortcut icon" href="/favicon.ico" type="image/x-icon" />

    <link rel="shortcut icon" href="/favicon.ico" type="image/x-icon" />
    <link rel="stylesheet" href="../css/font.css">
    <link rel="stylesheet" href="../css/xadmin.css">
    <link rel="stylesheet" href="../layui/css/layui.css" media="all">
    <link rel="stylesheet" href="../css/amazeui.min.css"/>

    <script src="../scripts/jquery.min.js"></script>
    <script src="../layui/layui.js" charset="utf-8"></script>
    <script type="text/javascript" src="../js/xadmin.js"></script>
    <title>学生成绩列表</title>
</head>

<body>

<input type="hidden" id="flag_manager" value=<%=session.getAttribute("flag_manager")%> />


<div class="x-body">
    <div class="layui-row">
        <div class="layui-form layui-col-md12 x-so" id="searchByInfo">
            <input class="layui-input" placeholder="资讯种类" name="info_class" id="info_class_input" onblur="search_blur()">
            <input type="datetime-local" class="layui-input" placeholder="最小时间" name="info_start_time" id="info_start_time" onblur="search_blur()">
            <input type="datetime-local" class="layui-input" placeholder="最大时间" name="info_end_time" id="info_end_time" autocomplete="off" onblur="search_blur()">
            <button class="layui-btn layui-btn-warm" onclick="searchByInfo()"><i class="layui-icon">&#xe615;</i>
            </button>
        </div>
    </div>
    <xblock>
        <button class="layui-btn layui-btn-danger" onclick="info_delete_many()"><i class="layui-icon"></i>批量删除</button>
        <button class="layui-btn" onclick="add_info_table()"><i class="layui-icon"></i>添加</button>
        <a href="">
        <span class="x-right" style="line-height:40px">
        <button class="layui-btn"><i class="layui-icon">&#xe65c;</i>刷新</button>
        </span>
        </a>
    </xblock>
    <table class="layui-table">
        <thead>
        <tr>
            <th>
                <div id="-1" class="layui-unselect header layui-form-checkbox" lay-skin="primary"><i class="layui-icon">&#xe605;</i></div>
            </th>
            <th>ID</th>
            <th>标题</th>
            <th>资讯内容</th>
            <th>发布时间</th>
            <th>类型</th>
            <th>操作</th>
        </thead>
        <tbody id="tbody_data_id">
        </tbody>
    </table>

    <!--page begin-->
    <div class="am-cf">
        共<span id="total_rows">1</span>条记录, 每页 <select id="page_nums" name="page_every" style="width: 70px;height: 20px;" onblur="select_blur()">
        <option selected="selected" value="5">05</option>
        <option value="10">10</option>
        <option value="15">15</option>
    </select> 条, 当前为  <input id="current_page" type="number" value="1" min="1" max="10" style="width: 50px;text-align: center" onblur="input_blur()" /> 页，
        共 <span id="total_pages">1</span> 页
        <div class="am-fr">
            <ul class="am-pagination">
                <li><a href="javascript:;" onclick="firstPage()" >首页</a></li>
                <li><a href="javascript:;" onclick="prevPage()" >上一页</a></li>
                <li><a href="javascript:;" onclick="nextPage()" >下一页</a></li>
                <li><a href="javascript:;" onclick="lastPage()" >尾页</a></li>
            </ul>
        </div>
    </div>
    <!--page end-->
</div>

<!--用户操作使用form表单提交信息-->
<form action="" id="sendOrder" name="sendOrder" method="post">
</form>
<!--设置用来接受弹出窗口的整个form表单-->
<div id="mianFromDiv" style="display:none;">
</div>
<!--弹框页面操作 end-->

<script>

    window.onload=function (){
       init();
    }

    function init(){
        $.ajax({
            url:"${pageContext.request.contextPath}/info_table_init.do",
            data:{
                "start_row":$("#current_page").val()*$("#page_nums").val()-$("#page_nums").val(),
                "page_nums":$("#page_nums").val()
            },
            success:function (data,status){
                if(status=="success"){
                    changeTotal(data);
                    changeRows(data);
                }
            }
        });
    }


    //layui自启动
    layui.use('layer', function(){
        $('.demo').on('click', function(){
            var type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
        });
    });


    //用户自定义相关函数
    function info_delete(info_id){
        layer.confirm('确定删除？', {btn: ['确定','取消']},
            function(){
                //发送删除的请求
                if($("#flag_manager").val()!="null"){
                $.ajax({
                    url:"${pageContext.request.contextPath}/info_table_delete.do",
                    data:{
                        "info_id":info_id,
                        "info_class":$("#info_class_input").val(),
                        "time_start":$("#info_start_time").val(),
                        "time_end":$("#info_end_time").val(),
                        "start_row":$("#current_page").val()*$("#page_nums").val()-$("#page_nums").val(),
                        "page_nums":$("#page_nums").val()
                    },
                    success:function(data,status){
                        if(status=="success"){
                            changeTotal(data);
                            changeRows(data);
                        }
                    }
                });
                layer.msg('已经删除', {icon: 1});
                }else{
                    layer.msg("操作失败，您不是管理员");
                }
            }, function(){
                layer.msg('取消删除', {});
            });
    }

    function info_delete_many(){
        layer.confirm('确定删除？', {btn: ['确定','取消']},
            function(){
                var info_ids=[];
                var all_selected=$(".layui-form-checked");
                if(all_selected.length==0){
                    layer.msg('您还没有选中任何选项');
                    return ;
                }else{
                    if(all_selected.length==1)
                        if(all_selected[0].id==-1) {
                            layer.msg('您还没有选中任何选项');
                            return ;
                        }
                }
                for(var i=0;i<all_selected.length;i++){
                    info_ids.push(all_selected[i].id);
                }
                if($("#flag_manager").val()!="null"){
                $.ajax({
                    url:"${pageContext.request.contextPath}/info_table_delete_many.do",
                    dataType:"json",
                    contentType:"application/json",
                    data:{
                        "info_ids":JSON.stringify(info_ids),
                        "info_class":$("#info_class_input").val(),
                        "time_start":$("#info_start_time").val(),
                        "time_end":$("#info_end_time").val(),
                        "start_row":$("#current_page").val()*$("#page_nums").val()-$("#page_nums").val(),
                        "page_nums":$("#page_nums").val()
                    },
                    success:function(data,status){
                        if(status=="success"){
                            changeTotal(data);
                            changeRows(data);
                        }
                    }
                });
                layer.msg('选中数据已被删除', {icon: 1});
                }else{
                    layer.msg("操作失败，您不是管理员");
                }
            }, function(){

                layer.msg('取消删除', {});
            });
    }

    //信息添加
    function stuInfo_add(){
        layer.open({
            type: 2,
            area: ['700px', '450px'],
            fixed: false, //不固定
            shadeClose: true,
            maxmin: true,
            content: '',
            end: function(){
                // 如果是通过单击关闭按钮关闭弹出层，父画面没有此表单
                if($("#subform").length === 1){
                    $("#subform").submit();
                }
            }
        });
    }

    //信息编辑
    function stuInfo_edit(obj,getid){
        layer.open({
            type: 2,
            area: ['700px', '450px'],
            fixed: false, //不固定
            shadeClose: true,
            maxmin: true,
            //跳出相对路劲，目前是在根目录下面
            content: '',
            end: function(){
                // 如果是通过单击关闭按钮关闭弹出层，父画面没有此表单
                if($("#subform").length === 1){
                    $("#subform").submit();
                }
            }
        });
    }

    //信息搜索
    function searchByInfo(){
        var info_class=$("#info_class_input").val();
        var start_time=$("#info_start_time").val();
        var end_time=$("#info_end_time").val();
        if(info_class==""&&start_time==""&&end_time=="")
          layer.msg('至少填写一个查询信息', {icon: 0.5});
        else
            layer.msg('查询成功');

    }

    //--------------------mysql数据库分页显示操作---------------------------------
    //首页

    function test1(){
        var info_class=$("#info_class_input").val();
        var start_time=$("#info_start_time").val();
        var end_time=$("#info_end_time").val();
        var str="info_class:"+info_class+"start_time:"+start_time+"end_time:"+end_time;
        alert(str);
    }
    function test2(id){
        alert(id);
    }

    function firstPage(){


            $.ajax({
                url:"${pageContext.request.contextPath}/info_table_split.do",
                data:{
                    "info_class":$("#info_class_input").val(),
                    "time_start":$("#info_start_time").val(),
                    "time_end":$("#info_end_time").val(),
                    "start_row":"0",
                    "page_nums":$("#page_nums").val()
                },
                success:function (data,status){
                    if(status=="success"){
                        $("#current_page").val(1);

                        changeRows(data);
                    }
                }
            });

    }
    function nextPage(){
        if($("#current_page").val()>=$("#total_pages").text()){
            layer.msg("这已经是最后一页了");
        }else{
            $.ajax({
                url:"${pageContext.request.contextPath}/info_table_split.do",
                data:{
                    "info_class":$("#info_class_input").val(),
                    "time_start":$("#info_start_time").val(),
                    "time_end":$("#info_end_time").val(),
                    "start_row":$("#current_page").val()*$("#page_nums").val(),
                    "page_nums":$("#page_nums").val()
                },
                success:function (data,status){
                    if(status=="success"){
                        var new_current_page=$("#current_page").val();
                        new_current_page++;
                        $("#current_page").val(new_current_page);

                        changeRows(data);
                    }
                }
            });
        }
    }
    function prevPage(){
        if($("#current_page").val()<=1){
            layer.msg("这已经是第一页了");
        }else{
            $.ajax({
                url:"${pageContext.request.contextPath}/info_table_split.do",
                data:{
                    "info_class":$("#info_class_input").val(),
                    "time_start":$("#info_start_time").val(),
                    "time_end":$("#info_end_time").val(),
                    "start_row":$("#current_page").val()*$("#page_nums").val()-$("#page_nums").val()*2,
                    "page_nums":$("#page_nums").val()
                },
                success:function (data,status){
                    if(status=="success"){
                        var new_current_page=$("#current_page").val();
                        new_current_page--;
                        $("#current_page").val(new_current_page);

                        changeRows(data);
                    }
                }
            });
        }
    }
    function lastPage(){

            $.ajax({
                url:"${pageContext.request.contextPath}/info_table_split.do",
                data:{
                    "info_class":$("#info_class_input").val(),
                    "time_start":$("#info_start_time").val(),
                    "time_end":$("#info_end_time").val(),
                    "start_row":$("#total_pages").text()*$("#page_nums").val()-$("#page_nums").val(),
                    "page_nums":$("#page_nums").val()
                },
                success:function (data,status){
                    if(status=="success"){
                        $("#current_page").val($("#total_pages").text());

                        changeRows(data);
                    }
                }
            });
    }

    function changeRows(data){
        $("#tbody_data_id").empty();
        for(var i=0;i<data.length-1;i++){
            var tr=$("<tr></tr>");
            var div_head=$("<div></div>").attr({
                "id":data[i].info_id,
                "class":"layui-unselect layui-form-checkbox",
                "lay-skin":"primary",
                "onclick":"checkBox_click("+data[i].info_id+")",
            });
            var i1=$("<i></i>").attr("class","layui-icon").html("&#xe605;");
            div_head.append(i1);
            var td1=$("<td></td>");
            td1.append(div_head);
            tr.append(td1);
            var td_tool1=$("<td></td>").text(data[i].info_id);
            var td_tool2=$("<td></td>").text(data[i].info_title);
            var td_tool3=$("<td></td>");
            var a_in_td=$("<a></a>").attr("href","javascript:edit_info_table("+data[i].info_id+");").text(data[i].info_content.substring(0,5)+"...");
            td_tool3.append(a_in_td);
            var info_time=new Date(parseInt(data[i].info_time)).toLocaleString().replace(/:\d{1,2}$/,' ');
            var td_tool4=$("<td></td>").text(info_time);
            var td_tool5=$("<td></td>").text(data[i].info_class);
            tr.append(td_tool1,td_tool2,td_tool3,td_tool4,td_tool5);


            var td2=$("<td></td>").attr("class","td-manage");
            var a1=$("<a></a>").attr({
                "title":"编辑",
                "href":"javascript:edit_info_table("+data[i].info_id+");"
            });
            var i1=$("<i></i>").attr("class","layui-icon").html("&#xe642;");
            a1.append(i1);

            var a2=$("<a></a>").attr({
                "title":"删除",
                "href":"javascript:info_delete("+data[i].info_id+");"
            });
            var i2=$("<i></i>").attr("class","layui-icon").html("&#xe640;");
            a2.append(i2);
            td2.append(a1,a2);
            tr.append(td2);
            $("#tbody_data_id").append(tr);
        }
    }
    function changeTotal(data){
        var total_rows=data[data.length-1].info_id;
        $("#total_rows").text(total_rows);
        var total_pages=Math.ceil(total_rows/$("#page_nums").val());
        $("#total_pages").text(total_pages);
        $("#current_page").attr("max",total_pages);
        // if(parseInt($("#current_page").val())>total_pages)
        //     $("#current_page").val(total_pages);
    }
    function checkBox_click(i){
        var id="#"+i;
        if($(id).hasClass('layui-form-checked')){
            $(id).removeClass('layui-form-checked');
        }else{
            $(id).addClass('layui-form-checked');
        }
    }

    function select_blur(){
            var total_rows=$("#total_rows").text();
            var total_pages=Math.ceil(total_rows/$("#page_nums").val());
            $("#total_pages").text(total_pages);
            $("#current_page").attr("max",total_pages);
            var current_page=$("#current_page").val();
            if(current_page>total_pages){
                current_page=total_pages;
                $("#current_page").val(total_pages);
            }
            $.ajax({
                url:"${pageContext.request.contextPath}/info_table_split.do",
                data:{
                    "info_class":$("#info_class_input").val(),
                    "time_start":$("#info_start_time").val(),
                    "time_end":$("#info_end_time").val(),
                    "start_row":current_page*$("#page_nums").val()-$("#page_nums").val(),
                    "page_nums":$("#page_nums").val()
                },
                success:function (data,status){
                    if(status=="success"){
                        changeRows(data);
                    }
                }
            });

    }
    function input_blur(){

        var total_pages=parseInt($("#total_pages").text());
        var current_page=parseInt($("#current_page").val());
        if(current_page>total_pages||current_page<1){
            layer.msg("输入的页数不对");
        }else{

                $.ajax({
                    url:"${pageContext.request.contextPath}/info_table_split.do",
                    data:{
                        "info_class":$("#info_class_input").val(),
                        "time_start":$("#info_start_time").val(),
                        "time_end":$("#info_end_time").val(),
                        "start_row":current_page*$("#page_nums").val()-$("#page_nums").val(),
                        "page_nums":$("#page_nums").val()
                    },
                    success:function (data,status){
                        if(status=="success"){

                            changeRows(data);
                        }
                    }
                });

        }
    }
    function search_blur(){

        $.ajax({
            url:"${pageContext.request.contextPath}/info_table_split.do",
            data:{
                "info_class":$("#info_class_input").val(),
                "time_start":$("#info_start_time").val(),
                "time_end":$("#info_end_time").val(),
                "start_row":"0",
                "page_nums":$("#page_nums").val()
            },
            success:function (data,status){
                if(status=="success"){
                    changeTotal(data);
                    $("#current_page").val(1).attr("max",$("#total_pages").text());
                    changeRows(data);
                }
            }
        });
    }

    function add_info_table(){
            var w=($(window).width()*0.9);
            var h=($(window).height() - 50);

        layer.open({
            type: 2,
            area: [w+'px', h +'px'],
            fix: false, //不固定
            maxmin: true,
            shadeClose: true,
            shade:0.4,
            title: "添加资讯信息",
            content: "info_table_add.jsp",
            // end:function(){
            //    var info_title=layer.getChildFrame('#info_title',index);
            //    alert(info_title.val());
            // }
            cancel: function(index,layero){
                var flag_add=layer.getChildFrame('#flag_add',index).val();
                if(flag_add=="yes") {
                    $.ajax({
                        url:"${pageContext.request.contextPath}/info_table_init.do",
                        data:{
                            "start_row":$("#current_page").val()*$("#page_nums").val()-$("#page_nums").val(),
                            "page_nums":$("#page_nums").val()
                        },
                        success:function (data,status){
                            if(status=="success"){
                                changeTotal(data);
                                lastPage();
                            }
                        }
                    });

                }
            }
        });
    }

    function edit_info_table(info_id){
        var w=($(window).width()*0.9);
        var h=($(window).height() - 50);

        layer.open({
            type: 2,
            area: [w+'px', h +'px'],
            fix: false, //不固定
            maxmin: true,
            shadeClose: true,
            shade:0.4,
            title: "修改资讯信息",
            content: "info_table_edit.jsp?info_id="+info_id,
            cancel: function(index,layero){
                var flag_edit=layer.getChildFrame('#flag_edit',index).val();
                if(flag_edit=="yes") {
                    $.ajax({
                        url:"${pageContext.request.contextPath}/info_table_split.do",
                        data:{
                            "info_class":$("#info_class_input").val(),
                            "time_start":$("#info_start_time").val(),
                            "time_end":$("#info_end_time").val(),
                            "start_row":$("#current_page").val()*$("#page_nums").val()-$("#page_nums").val(),
                            "page_nums":$("#page_nums").val()
                        },
                        success:function (data,status){
                            if(status=="success"){
                                changeRows(data);
                            }
                        }
                    });
                }
            }
        });

    }
</script>
</body>
</html>

